﻿using SqlSugar;

namespace WorkTool2.Entities
{
    public class DbConnectionEntity : BaseEntity
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// ip地址
        /// </summary>
        public string IPAddress { get; set; }

        /// <summary>
        /// 端口
        /// </summary>
        public int Port { get; set; }

        /// <summary>
        /// 用户
        /// </summary>
        public string User { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 默认数据库名称
        /// </summary>
        public string? DefaultDbName { get; set; }

        /// <summary>
        /// 数据库类型
        /// </summary>
        public DbType DbType { get; set; }

        /// <summary>
        /// 数据库连接
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public string DbConnection
        {
            get
            {
                var connStr = string.Empty;
                switch (DbType)
                {
                    case DbType.MySQL:
                        connStr = $"server={IPAddress};port={Port};user={User};password={Password};";
                        break;

                    case DbType.SQLServer:
                        connStr = $"server={IPAddress},{Port};User Id={User};Password={Password};Encrypt=True;TrustServerCertificate=True;";
                        break;

                    default:
                        break;
                }
                return connStr;
            }
        }
    }

    public enum DbType
    {
        MySQL,
        SQLServer,
    }
}